package day23;

import org.junit.Test;

import java.io.File;
import java.io.FilenameFilter;

public class Demo2 {
    public static void main(String[] args) {
        File file = new File("D:/a/b/c");
        String[] name = file.list();
        for(String s : name) {
            System.out.println(s);
        }
        File[] files = file.listFiles();
        for(File f : files) {
            System.out.println(f);
        }
    }
    @Test
    public void test2() {
        File file = new File("D:/a/b/c");
        String[] names = file.list(((dir, name) -> {
            if(name.endsWith(".md")) {
                return true;
            }
            return false;
        }));
        for(String s : names) {
            System.out.println(s);
        }
    }
    @Test
    public void test3() {
        File f = new File("D://a//b//c");
        getChild(f);
    }

    public void getChild(File f){
        File[] fs = f.listFiles();
        for(File file : fs) {
            if(file.isFile()) {
                System.out.println(file.getName());
            }else {
                getChild(file);
            }
        }
    }

    @Test
    public void test4() {
        File f = new File("D://a//b//c");
        deleteFile(f);
    }
    public void deleteFile(File f) {
        File[] fs= f.listFiles();
        //f文件  f空目录  f非空目录
        if(fs!=null&&fs.length>0) {
            for(File file : fs) {
                deleteFile(file);
            }
        }
        boolean b = f.delete();
        if(b) {System.out.println("删除成功！！！");}
    }
}
